package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Mdzx;
import cn.stylefeng.guns.sys.modular.system.entity.ZxYusuan;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxYusuanParam;
import cn.stylefeng.guns.sys.modular.system.service.ExportService;
import cn.stylefeng.guns.sys.modular.system.service.MdzxService;
import cn.stylefeng.guns.sys.modular.system.service.ZxYusuanService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 装修预算表控制器
 *
 * @author jiangge
 * @Date 2020-11-02 17:07:04
 */
@Controller
@RequestMapping("/zxYusuan")
public class ZxYusuanController extends BaseController {

    private String PREFIX = "/modular/system/zxYusuan";

    @Autowired
    private ZxYusuanService zxYusuanService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private MdzxService mdzxService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zxYusuan.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxYusuan_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zxYusuan_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxYusuanParam zxYusuanParam) {
        this.zxYusuanService.add(zxYusuanParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxYusuanParam zxYusuanParam) {
        this.zxYusuanService.update(zxYusuanParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxYusuanParam zxYusuanParam) {
        this.zxYusuanService.delete(zxYusuanParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxYusuanParam zxYusuanParam) {
        ZxYusuan detail = this.zxYusuanService.getById(zxYusuanParam.getBtyZxYusuanId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxYusuanParam zxYusuanParam) {
        return this.zxYusuanService.findPageBySpec(zxYusuanParam);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/listByYaoma")
    public LayuiPageInfo listByYaoma(ZxYusuanParam zxYusuanParam) {
        return this.zxYusuanService.findPageBySpec(zxYusuanParam);
    }


    // 导出excel
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response, Long btyZxMdzxId) {
        return exportService.exportExcel(request, response, btyZxMdzxId);
    }

    // 导出excel
    @RequestMapping("/listExports")
    public ResponseEntity<byte[]> exportExcels(HttpServletRequest request, HttpServletResponse response, @RequestParam String  ids) {
        String[] btyZxMdzxIds = ids.split(",");
        return exportService.exportExcels(request, response, btyZxMdzxIds);
    }

    @RequestMapping("/getMaxXuhao")
    @ResponseBody
    public ResponseData getMaxXuhao(ZxYusuanParam zxYusuanParam) {
        Integer maxId = this.zxYusuanService.getMaxXuhaoByMdId(zxYusuanParam.getBtyZxMdzxId());
        if (maxId == null) {
            maxId = 0;
        }
        maxId++;
        return ResponseData.success(maxId);
    }

    /**
     * 门店装修-预算导入
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public ResponseData ddpx(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxYusuanService.batchImport(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }

    /**
     * 复制预算页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/copyIndex")
    public String copyIndex() {
        return PREFIX + "/zxYusuan_copyIndex.html";
    }

    /**
     * 门店装修-查看预算
     *
     * @author jiangge
     * @Date 2020-11-27
     */
    @RequestMapping("/viewYuanSuan")
    public String viewYuanSuan(Model model, ZxYusuanParam param) {
        return PREFIX + "/viewYuanSuan.html";
    }

    /**
     * 门店装修-查看预算
     *
     * @author jiangge
     * @Date 2020-11-27
     */
    @RequestMapping("/viewYuanSuanByYaoma")
    public String viewYuanSuanByYaoma(Model model, ZxYusuanParam param) {
        return PREFIX + "/viewYuanSuanByYaoma.html";
    }


    /**
     * 初始化预算数据
     *
     * @author jiangge
     * @Date 2020-11-02
     *
     * 1 增加 校验，设置了预计投资，不能初始化； by zhouwanfeng 20210715
     *
     */
    @RequestMapping("/initYusuanData")
    @ResponseBody
    public ResponseData initYusuanData(ZxYusuanParam zxYusuanParam) {
        //判断是否设置预计投资
        Mdzx mdzx = mdzxService.getById(zxYusuanParam.getBtyZxMdzxId());
        if(mdzx.getYujiTouzi()!=null && mdzx.getYujiTouzi().doubleValue()>0){
            return new ErrorResponseData("该门店设置<预计投资>，不能重新初始化！");
        }
        ResponseData responseData=this.zxYusuanService.initYusuanData(zxYusuanParam);
        return responseData;
    }


}


